﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Client
{
    public enum SceneType 
    {
        Intro, Login, SelectCountry,
        SelectChr, NewChr, Loading,
        LoginNotice, PlayGame
    }

    #region Scene
    /// <summary>
    /// Base class that all scenes should inherit from.
    /// </summary>
    public class Scene
    {
        public SceneType SceneType;

        public Scene(SceneType scenetype)
        {
            SceneType = scenetype;
        }

        /// <summary>
        /// Initialize a scene.
        /// </summary>
        public virtual void Initializer() { }
        /// <summary>
        /// Finalize a scene.
        /// </summary>
        public virtual void Finalizer() { }
        public virtual void OpenScene() { }
        public virtual void CloseScene() { }
        public virtual void OpeningScene() { }
        public virtual void PlayScene(Sprite gameSprite) { }


        public virtual void KeyPress(KeyPressEventArgs e) { }
        public virtual void KeyDown(KeyEventArgs e) { }
        public virtual void MouseMove(MouseEventArgs e) { }
        public virtual void MouseDown(MouseEventArgs e) { }
    } 
    #endregion

    #region IntroScene
    /// <summary>
    /// This class is for when the game is starting up, can perform
    /// introduction movie and such.
    /// </summary>
    public sealed class IntroScene : Scene
    {
        public IntroScene()
            : base(SceneType.Intro)
        {
        }

        public override void OpenScene() { }
        public override void CloseScene() { }
        public override void PlayScene(Sprite gameSprite) { }
    }
    #endregion

    #region LoginScene
    public sealed class LoginScene : Scene
    {
        public enum LoginState { Login, NewId, NewIdRetry, ChgPw, CloseAll }

        private List<TextBox> ControlList;

        private TextBox EdId;
        private TextBox EdPasswd;

        private TextBox EdNewId;
        private TextBox EdNewPasswd;
        private TextBox EdConfirm;
        private TextBox EdYourName;
        private TextBox EdSSNo;
        private TextBox EdBirthDay;
        private TextBox EdQuiz1;
        private TextBox EdAnswer1;
        private TextBox EdQuiz2;
        private TextBox EdAnswer2;
        private TextBox EdPhone;
        private TextBox EdMobPhone;
        private TextBox EdEMail;

        private TextBox EdChgId;
        private TextBox EdChgCurrentPw;
        private TextBox EdChgNewPw;
        private TextBox EdChgRepeat;

        private int CurFrame, MaxFrame;
        private int StartTime; //ÇÑ ÇÁ·¡ÀÓ´ç ½Ã°£
        private bool NowOpening;
        private bool OpenFirst;
        private List<string> HelpHintList;
        //private TUserEntryInfo NewIdRetryUE;
        //private TUserEntryAddInfo NewIdRetryAdd;

        public bool UpdateAccountMode;


        public string LoginId { get; set; }
        public string LoginPasswd { get; set; }

        public LoginScene()
            : base(SceneType.Login)
        {
            ControlList = new List<TextBox>();
            HelpHintList = new List<string>();

            //·Î±×ÀÎ ¾ÆÀÌµð ÀÔ·Â
            EdId = new TextBox();
            // 2003/04/01 ÃÖ´ë LoginID ±æÀÌ Áõ°¡ 10->20 (¹Ì¸£3°èÁ¤ Æ÷ÇÔ)
            //EdId.Parent = FrmMain;
            //EdId.Font.Size = 10;
            EdId.MaxLength = 20;
            EdId.BorderStyle = BorderStyle.None;
            EdId.BackColor = Color.Black;
            EdId.ForeColor = Color.White;
            EdId.KeyPress += new KeyPressEventHandler(EdLoginIdKeyPress);
            EdId.Visible = false;
            EdId.Tag = 10;
            ControlList.Add(EdId);

            //·Î±×ÀÎ ÆÐ½º¿öµå ÀÔ·Â
            EdPasswd = new TextBox();
            //EdPasswd.Parent = FrmMain;
            //EdPasswd.Font.Size = 10;
            EdPasswd.MaxLength = 10;
            EdPasswd.BorderStyle = BorderStyle.None;
            EdPasswd.BackColor = Color.Black;
            EdPasswd.ForeColor = Color.White;
            EdPasswd.PasswordChar = '*';
            EdPasswd.KeyPress += new KeyPressEventHandler(EdLoginPasswdKeyPress);
            EdPasswd.Visible = false;
            EdPasswd.Tag = 10;
            ControlList.Add(EdPasswd);

            int nx = 79;
            int ny = 64;
            EdNewId = new TextBox();
            //EdNewId.Parent = FrmMain;
            EdNewId.Height = 16;
            EdNewId.Width = 116;
            EdNewId.Left = nx + 161;
            EdNewId.Top = ny + 116;
            EdNewId.BorderStyle = BorderStyle.None;
            EdNewId.BackColor = Color.Black;
            EdNewId.ForeColor = Color.White;
            EdNewId.MaxLength = 10;
            EdNewId.Visible = false;
            EdNewId.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdNewId.Enter += new EventHandler(EdNewOnEnter);
            EdNewId.Tag = 11;
            ControlList.Add(EdNewId);

            EdNewPasswd = new TextBox();
            //EdNewPasswd.Parent = FrmMain;
            EdNewPasswd.Height = 16;
            EdNewPasswd.Width = 116;
            EdNewPasswd.Left = nx + 161;
            EdNewPasswd.Top = ny + 137;
            EdNewPasswd.BorderStyle = BorderStyle.None;
            EdNewPasswd.BackColor = Color.Black;
            EdNewPasswd.ForeColor = Color.White;
            EdNewPasswd.MaxLength = 10;
            EdNewPasswd.PasswordChar = '*';
            EdNewPasswd.Visible = false;
            EdNewPasswd.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdNewPasswd.Enter += new EventHandler(EdNewOnEnter);
            EdNewPasswd.Tag = 11;
            ControlList.Add(EdNewPasswd);

            EdConfirm = new TextBox();
            //EdConfirm.Parent = FrmMain;
            EdConfirm.Height = 16;
            EdConfirm.Width = 116;
            EdConfirm.Left = nx + 161;
            EdConfirm.Top = ny + 158;
            EdConfirm.BorderStyle = BorderStyle.None;
            EdConfirm.BackColor = Color.Black;
            EdConfirm.ForeColor = Color.White;
            EdConfirm.MaxLength = 10;
            EdConfirm.PasswordChar = '*';
            EdConfirm.Visible = false;
            EdConfirm.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdConfirm.Enter += new EventHandler(EdNewOnEnter);
            EdConfirm.Tag = 11;
            ControlList.Add(EdConfirm);

            EdYourName = new TextBox();
            //EdYourName.Parent = FrmMain;
            EdYourName.Height = 16;
            EdYourName.Width = 116;
            EdYourName.Left = nx + 161;
            EdYourName.Top = ny + 187;
            EdYourName.BorderStyle = BorderStyle.None;
            EdYourName.BackColor = Color.Black;
            EdYourName.ForeColor = Color.White;
            EdYourName.MaxLength = 20;
            EdYourName.Visible = false;
            EdYourName.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdYourName.Enter += new EventHandler(EdNewOnEnter);
            EdYourName.Tag = 11;
            ControlList.Add(EdYourName);

            EdSSNo = new TextBox();
            //EdSSNo.Parent = FrmMain;
            EdSSNo.Height = 16;
            EdSSNo.Width = 116;
            EdSSNo.Left = nx + 161;
            EdSSNo.Top = ny + 207;
            EdSSNo.BorderStyle = BorderStyle.None;
            EdSSNo.BackColor = Color.Black;
            EdSSNo.ForeColor = Color.White;
            EdSSNo.MaxLength = 14;
            EdSSNo.Visible = false;
            EdSSNo.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdSSNo.Enter += new EventHandler(EdNewOnEnter);
            EdSSNo.Tag = 11;
            ControlList.Add(EdSSNo);

            EdBirthDay = new TextBox();
            //EdBirthDay.Parent = FrmMain;
            EdBirthDay.Height = 16;
            EdBirthDay.Width = 116;
            EdBirthDay.Left = nx + 161;
            EdBirthDay.Top = ny + 227;
            EdBirthDay.BorderStyle = BorderStyle.None;
            EdBirthDay.BackColor = Color.Black;
            EdBirthDay.ForeColor = Color.White;
            EdBirthDay.MaxLength = 10;
            EdBirthDay.Visible = false;
            EdBirthDay.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdBirthDay.Enter += new EventHandler(EdNewOnEnter);
            EdBirthDay.Tag = 11;
            ControlList.Add(EdBirthDay);

            EdQuiz1 = new TextBox();
            //EdQuiz1.Parent = FrmMain;
            EdQuiz1.Height = 16;
            EdQuiz1.Width = 163;
            EdQuiz1.Left = nx + 161;
            EdQuiz1.Top = ny + 256;
            EdQuiz1.BorderStyle = BorderStyle.None;
            EdQuiz1.BackColor = Color.Black;
            EdQuiz1.ForeColor = Color.White;
            EdQuiz1.MaxLength = 20;
            EdQuiz1.Visible = false;
            EdQuiz1.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdQuiz1.Enter += new EventHandler(EdNewOnEnter);
            EdQuiz1.Tag = 11;
            ControlList.Add(EdQuiz1);

            EdAnswer1 = new TextBox();
            //EdAnswer1.Parent = FrmMain;
            EdAnswer1.Height = 16;
            EdAnswer1.Width = 163;
            EdAnswer1.Left = nx + 161;
            EdAnswer1.Top = ny + 276;
            EdAnswer1.BorderStyle = BorderStyle.None;
            EdAnswer1.BackColor = Color.Black;
            EdAnswer1.ForeColor = Color.White;
            EdAnswer1.MaxLength = 12;
            EdAnswer1.Visible = false;
            EdAnswer1.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdAnswer1.Enter += new EventHandler(EdNewOnEnter);
            EdAnswer1.Tag = 11;
            ControlList.Add(EdAnswer1);

            EdQuiz2 = new TextBox();
            //EdQuiz2.Parent = FrmMain;
            EdQuiz2.Height = 16;
            EdQuiz2.Width = 163;
            EdQuiz2.Left = nx + 161;
            EdQuiz2.Top = ny + 297;
            EdQuiz2.BorderStyle = BorderStyle.None;
            EdQuiz2.BackColor = Color.Black;
            EdQuiz2.ForeColor = Color.White;
            EdQuiz2.MaxLength = 20;
            EdQuiz2.Visible = false;
            EdQuiz2.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdQuiz2.Enter += new EventHandler(EdNewOnEnter);
            EdQuiz2.Tag = 11;
            ControlList.Add(EdQuiz2);

            EdAnswer2 = new TextBox();
            //EdAnswer2.Parent := FrmMain;
            EdAnswer2.Height = 16;
            EdAnswer2.Width = 163;
            EdAnswer2.Left = nx + 161;
            EdAnswer2.Top = ny + 317;
            EdAnswer2.BorderStyle = BorderStyle.None;
            EdAnswer2.BackColor = Color.Black;
            EdAnswer2.ForeColor = Color.White;
            EdAnswer2.MaxLength = 12;
            EdAnswer2.Visible = false;
            EdAnswer2.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdAnswer2.Enter += new EventHandler(EdNewOnEnter);
            EdAnswer2.Tag = 11;
            ControlList.Add(EdAnswer2);

            EdPhone = new TextBox();
            //EdPhone.Parent := FrmMain;
            EdPhone.Height = 16;
            EdPhone.Width = 116;
            EdPhone.Left = nx + 161;
            EdPhone.Top = ny + 347;
            EdPhone.BorderStyle = BorderStyle.None;
            EdPhone.BackColor = Color.Black;
            EdPhone.ForeColor = Color.White;
            EdPhone.MaxLength = 14;
            EdPhone.Visible = false;
            EdPhone.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdPhone.Enter += new EventHandler(EdNewOnEnter);
            EdPhone.Tag = 11;
            ControlList.Add(EdPhone);

            EdMobPhone = new TextBox();
            //EdMobPhone.Parent := FrmMain;
            EdMobPhone.Height = 16;
            EdMobPhone.Width = 116;
            EdMobPhone.Left = nx + 161;
            EdMobPhone.Top = ny + 368;
            EdMobPhone.BorderStyle = BorderStyle.None;
            EdMobPhone.BackColor = Color.Black;
            EdMobPhone.ForeColor = Color.White;
            EdMobPhone.MaxLength = 13;
            EdMobPhone.Visible = false;
            EdMobPhone.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdMobPhone.Enter += new EventHandler(EdNewOnEnter);
            EdMobPhone.Tag = 11;
            ControlList.Add(EdMobPhone);

            EdEMail = new TextBox();
            //EdEMail.Parent = FrmMain;
            EdEMail.Height = 16;
            EdEMail.Width = 116;
            EdEMail.Left = nx + 161;
            EdEMail.Top = ny + 388;
            EdEMail.BorderStyle = BorderStyle.None;
            EdEMail.BackColor = Color.Black;
            EdEMail.ForeColor = Color.White;
            EdEMail.MaxLength = 40;
            EdEMail.Visible = false;
            EdEMail.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdEMail.Enter += new EventHandler(EdNewOnEnter);
            EdEMail.Tag = 11;
            ControlList.Add(EdEMail);

            nx = 192;
            ny = 150;
            EdChgId = new TextBox();
            //EdChgId.Parent = FrmMain;
            EdChgId.Height = 16;
            EdChgId.Width = 137;
            EdChgId.Left = nx + 239;
            EdChgId.Top = ny + 117;
            EdChgId.BorderStyle = BorderStyle.None;
            EdChgId.BackColor = Color.Black;
            EdChgId.ForeColor = Color.White;
            EdChgId.MaxLength = 10;
            EdChgId.Visible = false;
            EdChgId.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdChgId.Enter += new EventHandler(EdNewOnEnter);
            EdChgId.Tag = 12;
            ControlList.Add(EdChgId);

            EdChgCurrentPw = new TextBox();
            //EdChgCurrentPw.Parent = FrmMain;
            EdChgCurrentPw.Height = 16;
            EdChgCurrentPw.Width = 137;
            EdChgCurrentPw.Left = nx + 239;
            EdChgCurrentPw.Top = ny + 149;
            EdChgCurrentPw.BorderStyle = BorderStyle.None;
            EdChgCurrentPw.BackColor = Color.Black;
            EdChgCurrentPw.ForeColor = Color.White;
            EdChgCurrentPw.MaxLength = 10;
            EdChgCurrentPw.PasswordChar = '*';
            EdChgCurrentPw.Visible = false;
            EdChgCurrentPw.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdChgCurrentPw.Enter += new EventHandler(EdNewOnEnter);
            EdChgCurrentPw.Tag = 12;
            ControlList.Add(EdChgCurrentPw);

            EdChgNewPw = new TextBox();
            //EdChgNewPw.Parent := FrmMain;
            EdChgNewPw.Height = 16;
            EdChgNewPw.Width = 137;
            EdChgNewPw.Left = nx + 239;
            EdChgNewPw.Top = ny + 176;
            EdChgNewPw.BorderStyle = BorderStyle.None;
            EdChgNewPw.BackColor = Color.Black;
            EdChgNewPw.ForeColor = Color.White;
            EdChgNewPw.MaxLength = 10;
            EdChgNewPw.PasswordChar = '*';
            EdChgNewPw.Visible = false;
            EdChgNewPw.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdChgNewPw.Enter += new EventHandler(EdNewOnEnter);
            EdChgNewPw.Tag = 12;
            ControlList.Add(EdChgNewPw);

            EdChgRepeat = new TextBox();
            //EdChgRepeat.Parent := FrmMain;
            EdChgRepeat.Height = 16;
            EdChgRepeat.Width = 137;
            EdChgRepeat.Left = nx + 239;
            EdChgRepeat.Top = ny + 208;
            EdChgRepeat.BorderStyle = BorderStyle.None;
            EdChgRepeat.BackColor = Color.Black;
            EdChgRepeat.ForeColor = Color.White;
            EdChgRepeat.MaxLength = 10;
            EdChgRepeat.PasswordChar = '*';
            EdChgRepeat.Visible = false;
            EdChgRepeat.KeyPress += new KeyPressEventHandler(EdNewIdKeyPress);
            EdChgRepeat.Enter += new EventHandler(EdNewOnEnter);
            EdChgRepeat.Tag = 12;
            ControlList.Add(EdChgRepeat);
        }

        private void EdLoginIdKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                e.Handled = true;
                LoginId = EdId.Text.ToLower();
                if (LoginId != string.Empty)
                    EdPasswd.Focus();
            }
        }

        private void EdLoginPasswdKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                e.Handled = true;
                LoginId = EdId.Text.ToLower();
                LoginPasswd = EdPasswd.Text;

                if (LoginId != string.Empty && LoginPasswd != string.Empty)
                {
                    //°èÁ¤À¸·Î ·Î±×ÀÎ ÇÑ´Ù.
                    //FrmMain.SendLogin(LoginId, LoginPasswd);
                    LoginId = string.Empty;
                    LoginPasswd = string.Empty;
                    EdId.Text = string.Empty;
                    EdPasswd.Text = string.Empty;
                    EdId.Visible = false;
                    EdPasswd.Visible = false;
                }
                else if (EdId.Visible && EdId.Text == string.Empty)
                    EdId.Focus();
            }
        }

        private void EdNewOnEnter(object sender, EventArgs e)
        {
            //ÈùÆ®
            HelpHintList.Clear();
            int hx = ((TextBox)sender).Left + ((TextBox)sender).Width + 10;
            int hy = ((TextBox)sender).Top + ((TextBox)sender).Height - 18;

            if (sender == EdNewId)
            {
                HelpHintList.Add("Your ID can be a combination of");
                HelpHintList.Add("characters and numbers and");
                HelpHintList.Add("it must be a minimum of 4 letters.");
                HelpHintList.Add("Your ID is not your character");
                HelpHintList.Add("name in the game, Choose your ID");
                HelpHintList.Add("carefully, because it is essential");
                HelpHintList.Add(" to use all our services.");
                HelpHintList.Add("");
                HelpHintList.Add("We suggest you use a different");
                HelpHintList.Add("name from the one you would like");
                HelpHintList.Add("to use for your character.");
            }
            else if (sender == EdNewPasswd)
            {
                HelpHintList.Add("Your password can be a");
                HelpHintList.Add("combination of characters");
                HelpHintList.Add("and numbers and it must be a");
                HelpHintList.Add("minimum of 4 letters.");
                HelpHintList.Add("Remember that your password is");
                HelpHintList.Add("essential to play our game,");
                HelpHintList.Add("so be sure to make a note of it.");
                HelpHintList.Add("We advise you to not use");
                HelpHintList.Add("a simple password");
                HelpHintList.Add("to avoid the risk");
                HelpHintList.Add("of account hacking.");
            }
            else if (sender == EdConfirm)
            {
                HelpHintList.Add("type password again");
                HelpHintList.Add("for confirmation.");
            }
            else if (sender == EdYourName)
            {
                HelpHintList.Add("type your full name.");
            }
            else if (sender == EdSSNo)
            {
                HelpHintList.Add("Not used");
                HelpHintList.Add("at this time.");
            }
            else if (sender == EdBirthDay)
            {
                HelpHintList.Add("Please type your birth date, month,");
                HelpHintList.Add("years. ex)1975/08/21)");
            }
            else if (sender == EdQuiz1)
            {
                HelpHintList.Add("Please type a question only");
                HelpHintList.Add("you know the answer to.");
                HelpHintList.Add("");
            }
            else if (sender == EdAnswer1)
            {
                HelpHintList.Add("please type an answer to the");
                HelpHintList.Add("above question.");
            }
            else if (sender == EdQuiz2)
            {
                HelpHintList.Add("Please type a question only");
                HelpHintList.Add("you know the answer to.");
                HelpHintList.Add("");
            }
            else if (sender == EdAnswer2)
            {
                HelpHintList.Add("please type an answer to the");
                HelpHintList.Add("above question.");
            }
            else if ((sender == EdYourName) || (sender == EdSSNo) || (sender == EdQuiz1) || (sender == EdQuiz2) || (sender == EdAnswer1) || (sender == EdAnswer2))
            {
                HelpHintList.Add("You are solely responsible");
                HelpHintList.Add("for the information you give us");
                HelpHintList.Add("if you use false information,");
                HelpHintList.Add("");
                HelpHintList.Add("You will not be able to use");
                HelpHintList.Add("all our services.");
                HelpHintList.Add("Your account may be removed");
                HelpHintList.Add("if you provide ");
                HelpHintList.Add("false information.");
            }
            else if (sender == EdPhone)
            {
                HelpHintList.Add("Please type in your telephone");
                HelpHintList.Add("number(compulsory).");
            }
            else if (sender == EdMobPhone)
            {
                HelpHintList.Add("Your mobile telephone number");
            }
            else if (sender == EdEMail)
            {
                HelpHintList.Add("Please type your E-mail address.");
                HelpHintList.Add("Your E-mail will be used to access");
                HelpHintList.Add("some of our services. You can");
                HelpHintList.Add("receive latest update information.");
            }
        }

        private void EdNewIdKeyPress(object sender, KeyPressEventArgs e)
        {
            if ((sender == EdNewPasswd) || (sender == EdChgNewPw) || (sender == EdChgRepeat))
                if ((e.KeyChar == '~') || (e.KeyChar == '\0') || (e.KeyChar == ' '))
                    e.Handled = true;

            if (e.KeyChar == (char)Keys.Return)
            {
                e.Handled = true;
                if (sender == EdNewId)
                {
                    if (!NewIdCheckNewId())
                        return;
                }
                if (sender == EdNewPasswd)
                {
                    if (EdNewPasswd.Text.Length < 4)
                    {
                        //FrmDlg.DMessageDlg ('ºñ¹Ð¹øÈ£´Â 4±ÛÀÚ ÀÌ»óÀÌ¾î¾ß ÇÕ´Ï´Ù.', [mbOk]);
                        //Beep;
                        EdNewPasswd.Focus();
                        return;
                    }
                }
                if (sender == EdConfirm)
                {
                    if (EdNewPasswd.Text != EdConfirm.Text)
                    {
                        //FrmDlg.DMessageDlg ('ºñ¹Ð¹øÈ£ È®ÀÎÀÌ Æ²·È½À´Ï´Ù. ´Ù½Ã ÀÔ·ÂÇÏ½Ê½Ã¿À.', [mbOk]);
                        //Beep;
                        EdConfirm.Focus();
                        return;
                    }
                }
                if ((sender == EdYourName) || (sender == EdQuiz1) || (sender == EdAnswer1) ||
                  (sender == EdQuiz2) || (sender == EdAnswer2) || (sender == EdPhone) ||
                  (sender == EdMobPhone) || (sender == EdEMail))
                {
                    ((TextBox)sender).Text = ((TextBox)sender).Text.Trim();
                    if (((TextBox)sender).Text == string.Empty)
                    {
                        //Beep;
                        ((TextBox)sender).Focus();
                        return;
                    }
                }
                if (sender == EdSSNo /*&& KoreanVersion*/) //ÇÑ±¹ÀÎ °æ¿ì.. ÁÖ¹Îµî·Ï¹øÈ£ °£·« Ã¤Å©
                {
                    if (!NewIdCheckSSno())
                        return;
                }
                if (sender == EdBirthDay)
                {
                    if (!NewIdCheckBirthDay())
                        return;
                }
                if (((TextBox)sender).Text != string.Empty)
                {
                    if (sender == EdNewId)
                        EdNewPasswd.Focus();
                    if (sender == EdNewPasswd)
                        EdConfirm.Focus();
                    if (sender == EdConfirm)
                        EdYourName.Focus();
                    if (sender == EdYourName)
                        EdSSNo.Focus();
                    if (sender == EdSSNo)
                        EdBirthDay.Focus();
                    if (sender == EdBirthDay)
                        EdQuiz1.Focus();
                    if (sender == EdQuiz1)
                        EdAnswer1.Focus();
                    if (sender == EdAnswer1)
                        EdQuiz2.Focus();
                    if (sender == EdQuiz2)
                        EdAnswer2.Focus();
                    if (sender == EdAnswer2)
                        EdPhone.Focus();
                    if (sender == EdPhone)
                        EdMobPhone.Focus();
                    if (sender == EdMobPhone)
                        EdEMail.Focus();
                    if (sender == EdEMail)
                    {
                        if (EdNewId.Enabled)
                            EdNewId.Focus();
                        else if (EdNewPasswd.Enabled)
                            EdNewPasswd.Focus();
                    }

                    if (sender == EdChgId)
                        EdChgCurrentPw.Focus();
                    if (sender == EdChgCurrentPw)
                        EdChgNewPw.Focus();
                    if (sender == EdChgNewPw)
                        EdChgRepeat.Focus();
                    if (sender == EdChgRepeat)
                        EdChgId.Focus();
                }
            }
        }

        #region Data input checks
        private bool CheckUserEntrys()
        {
            EdNewId.Text = EdNewId.Text.Trim();
            EdQuiz1.Text = EdQuiz1.Text.Trim();
            EdYourName.Text = EdYourName.Text.Trim();
            if (!NewIdCheckNewId())
                return false;

            /*if (KoreanVersion) //¿µ¹® ¹öÀü¿¡¼­´Â Ã¼Å©¾ÈÇÔ
            {
                if (!NewIdCheckSSNo())
                    return false;
            }*/

            if (!NewIdCheckBirthDay())
                return false;

            if (EdNewId.Text.Length < 3)
            {
                EdNewId.Focus();
                return false;
            }
            if (EdNewPasswd.Text.Length < 3)
            {
                EdNewPasswd.Focus();
                return false;
            }
            if (EdNewPasswd.Text != EdConfirm.Text)
            {
                EdConfirm.Focus();
                return false;
            }
            if (EdQuiz1.Text.Length < 1)
            {
                EdQuiz1.Focus();
                return false;
            }
            if (EdAnswer1.Text.Length < 1)
            {
                EdAnswer1.Focus();
                return false;
            }
            if (EdQuiz2.Text.Length < 1)
            {
                EdQuiz2.Focus();
                return false;
            }
            if (EdAnswer2.Text.Length < 1)
            {
                EdAnswer2.Focus();
                return false;
            }
            if (EdYourName.Text.Length < 1)
            {
                EdYourName.Focus();
                return false;
            }

            /*if (KoreanVersion) //¿µ¹® ¹öÀü¿¡¼­´Â Ã¼Å©¾ÈÇÔ
            {
                if (EdSSNo.Text.Length < 1)
                {
                    EdSSNo.Focus();
                    return false;
                }
            }*/

            return true;
        }

        private bool NewIdCheckNewId()
        {
            EdNewId.Text = EdNewId.Text.Trim();
            if (EdNewId.Text.Length < 3)
            {
                //FrmDlg.DMessageDlg ("°èÁ¤ÀÌ ¾ÆÀÌµð´Â Àû¾îµµ 3±ÛÀÚ ÀÌ»óÀÌ¾î¾ß ÇÕ´Ï´Ù.", [mbOk]);
                //Beep;
                EdNewId.Focus();
                return false;
            }
            return true;
        }

        private bool NewIdCheckSSno()
        {
            string str, t1, t2, t3, syear, smon, sday;
            int ayear, amon, aday, sex;
            bool flag = true;

            str = EdSSNo.Text;
            ClientGlobals.PickString(str, out t1, 0, '-');
            ClientGlobals.PickString(str, out t2, 1, '-');

            if ((t1.Length == 6) && (t2.Length == 7))
            {
                smon = t1.Substring(2, 2);
                sday = t1.Substring(4, 2);
                amon = Convert.ToInt32(smon);
                aday = Convert.ToInt32(sday);
                if ((amon <= 0) || (amon > 12))
                    flag = false;
                if ((aday <= 0) || (aday > 31))
                    flag = false;
                sex = Convert.ToInt32(t2[0].ToString());
                if ((sex <= 0) || (sex > 2))
                    flag = false;
            }
            else
                flag = false;

            if (!flag)
            {
                // Beep;
                EdSSNo.Focus();
                return false;
            }

            return true;
        }

        private bool NewIdCheckBirthDay()
        {
            string str, syear, smon, sday;
            int ayear, amon, aday;
            bool flag = true;

            str = EdBirthDay.Text;
            ClientGlobals.PickString(str, out syear, 0, '/');
            ClientGlobals.PickString(str, out smon, 0, '/');
            ClientGlobals.PickString(str, out sday, 0, '/');
            ayear = Convert.ToInt32(syear);
            amon = Convert.ToInt32(smon);
            aday = Convert.ToInt32(sday);
            if ((ayear <= 1890) || (ayear > 2101))
                flag = false;
            if ((amon <= 0) || (amon > 12))
                flag = false;
            if ((aday <= 0) || (aday > 31))
                flag = false;

            if (!flag)
            {
                //Beep;
                EdBirthDay.Focus();
                return false;
            }

            return true;
        }
        #endregion

        public void ChangeLoginState(LoginState state)
        {
            int focus = -1;
            switch (state)
            {
                case LoginState.Login: focus = 10; break;
                case LoginState.NewIdRetry:
                case LoginState.NewId:
                    {
                        focus = 11;
                        break;
                    }
                case LoginState.ChgPw: focus = 12; break;
                case LoginState.CloseAll: focus = -1; break;
            }

            foreach (TextBox c in ControlList)
            {
                if ((int)c.Tag >= 10 && (int)c.Tag <= 12)
                {
                    if ((int)c.Tag == focus)
                    {
                        c.Visible = true;
                        c.Text = string.Empty;
                    }
                    else
                    {
                        c.Visible = false;
                        c.Text = string.Empty;
                    }
                }
            }
            //            if (!KoreanVersion) //¿µ¹®¹öÀüÀº ÁÖ¹Îµî·Ï¹øÈ£ ÀÔ·ÂÀ» ¾ÈÇÑ´Ù.
            //                EdSSNo.Visible = false;



            /*case state of
              lsLogin: begin
                  FrmDlg.DNewAccount.Visible := FALSE;
                  FrmDlg.DChgPw.Visible := FALSE;
                  FrmDlg.DLogin.Visible := TRUE;
                  if EdId.Visible)
                    EdId.Focus();
                end;
              lsNewIdRetry,
                lsNewId: begin
                  if BoUpdateAccountMode)
                    EdNewId.Enabled := FALSE
                  else
                    EdNewId.Enabled := TRUE;
                  FrmDlg.DNewAccount.Visible := TRUE;
                  FrmDlg.DChgPw.Visible := FALSE;
                  FrmDlg.DLogin.Visible := FALSE;
                  if EdNewId.Visible and EdNewId.Enabled then begin
                    EdNewId.Focus();
                  end else begin
                    if EdConfirm.Visible and EdConfirm.Enabled then
                      EdConfirm.Focus();
                  end;
                end;
              lsChgpw: begin
                  FrmDlg.DNewAccount.Visible := FALSE;
                  FrmDlg.DChgPw.Visible := TRUE;
                  FrmDlg.DLogin.Visible := FALSE;
                  if EdChgId.Visible then
                    EdChgId.Focus();
                end;
              lsCloseAll: begin
                  FrmDlg.DNewAccount.Visible := FALSE;
                  FrmDlg.DChgPw.Visible := FALSE;
                  FrmDlg.DLogin.Visible := FALSE;
                end;
            end;
          end;*/
        }

        public void NewClick()
        {
            UpdateAccountMode = false;
            //FrmDlg.NewAccountTitle := '';
            ChangeLoginState(LoginState.NewId);
        }

        //public void NewIdRetry(boupdate: Boolean);
        //public void UpdateAccountInfos(ue: TUserEntryInfo);

        public void OkClick()
        {
            //key := #13;
            //EdLoginPasswdKeyPress(self, key);
        }

        public void ChgPwClick()
        {
            ChangeLoginState(LoginState.ChgPw);
        }

        //public void NewAccountOk;

        public void NewAccountClose()
        {
            if (!UpdateAccountMode)
                ChangeLoginState(LoginState.Login);
        }

        public void ChgpwOk()
        {
            string uid, passwd, newpasswd;

            if (EdChgNewPw.Text == EdChgRepeat.Text)
            {
                uid = EdChgId.Text;
                passwd = EdChgCurrentPw.Text;
                newpasswd = EdChgNewPw.Text;
                //FrmMain.SendChgPw(uid, passwd, newpasswd);
                ChgpwCancel();
            }
            else
            {
                //FrmDlg.DMessageDlg('»õ ºñ¹Ð¹øÈ£ ÀçÀÔ·Â È®ÀÎÀÌ ¸ÂÁö ¾Ê½À´Ï´Ù.', [mbOk]);
                EdChgNewPw.Focus();
            }
        }

        public void ChgpwCancel()
        {
            ChangeLoginState(LoginState.Login);
        }

        public void HideLoginBox()
        {
            ChangeLoginState(LoginState.CloseAll);
        }

        public void OpenLoginDoor()
        {
            NowOpening = true;
            StartTime = Environment.TickCount;
            HideLoginBox();
            //PlaySound(s_rock_door_open);
        }

        public void PassWdFail()
        {
            EdId.Visible = true;
            EdPasswd.Visible = true;
            EdId.Focus();
        }

        public override void OpenScene()
        {
            CurFrame = 0;
            MaxFrame = 19;
            LoginId = string.Empty;
            LoginPasswd = string.Empty;

            EdId.Left = 330; //350;
            EdId.Top = 258; //259;
            EdId.Height = 16;
            EdId.Width = 137;
            EdId.Visible = false;

            EdPasswd.Left = 330; //350;
            EdPasswd.Top = 289; //291;
            EdPasswd.Height = 16;
            EdPasswd.Width = 137;
            EdPasswd.Visible = false;

            //----------------------------------------
            OpenFirst = true;

            //FrmDlg.DLogin.Visible := TRUE;
            //FrmDlg.DNewAccount.Visible := FALSE;
            NowOpening = false;
            //PlayBGM(bmg_intro);
        }

        public override void CloseScene()
        {
            EdId.Visible = false;
            EdPasswd.Visible = false;
            //FrmDlg.DLogin.Visible := false;
            //SilenceSound;
        }

        public override void PlayScene(Sprite gameSprite)
        {
            try
            {
                gameSprite.Begin(SpriteFlags.None);
                //if not ServerConnected then exit;
                if (OpenFirst)
                {
                    OpenFirst = false;
                    EdId.Visible = true;
                    EdPasswd.Visible = true;
                    EdId.Focus();
                }

                Texture d = ImageLib.CharacterSelect[0];
                if (d != null)
                    gameSprite.Draw(d, Rectangle.Empty, Vector3.Empty, Vector3.Empty, Color.White);

                // TODO!
                /*if NowOpening then begin
        if GetTickCount - StartTime > 120 then begin
          StartTime := GetTickCount;
          Inc(CurFrame);
        end;
        if CurFrame >= MaxFrame - 1 then begin
          CurFrame := MaxFrame - 1;
          if not DoFadeOut and not DoFadeIn then begin
            DoFadeOut := TRUE;
            DoFadeIn := TRUE;
            //            FadeIndex := 29;
            FadeIndex := 25;
          end;
        end;
        d := FrmMain.WChrSel.Images[CurFrame];
        if d <> nil then
          MSurface.Draw(0, 0, d.ClientRect, d, TRUE);
        //         MSurface.Draw (152, 96, d.ClientRect, d, TRUE);

        if DoFadeOut then begin
          if FadeIndex <= 1 then begin
            FrmMain.WProgUse.ClearCache;
            FrmMain.WChrSel.ClearCache;
            Sleep(500);
            DScreen.ChangeScene(stSelectChr);
              //¼­¹ö¿¡¼­ Ä³¸¯ÅÍ Á¤º¸°¡ ¿À¸é ¼±ÅÃÃ¢À¸·Î ³Ñ¾î°£´Ù.
          end;
        end;
      end;*/
            }
            finally
            {
                gameSprite.End();
            }
        }
    }
	#endregion

    #region SceneManager
    public sealed class SceneManager
    {
        public static readonly SceneManager Me = new SceneManager();

        // the scenes that we will use - these are all static for convenience.
        public static readonly IntroScene IntroScene = new IntroScene();
        public static readonly LoginScene LoginScene = new LoginScene();
        public static readonly GamePlayScene PlayScene = new GamePlayScene();

        private int LastTick;
        private int LastFrameRate;
        private int FrameRate;
        public Scene CurrentScene;

        public int CurrentFPS { get { return LastFrameRate; } }

        private SceneManager()
            : base()
        {
            LastTick = Environment.TickCount;
            LastFrameRate = 0;
            FrameRate = 0;
            CurrentScene = null;
        }

        public void Initializer()
        {
            IntroScene.Initializer();
            LoginScene.Initializer();
            PlayScene.Initializer();
        }

        public void Finalizer()
        {
            IntroScene.Finalizer();
            LoginScene.Finalizer();
            PlayScene.Finalizer();
        }

        public void ChangeScene(SceneType st)
        {
            if (CurrentScene != null)
                CurrentScene.CloseScene();

            switch (st)
            {
                case SceneType.Intro: CurrentScene = IntroScene; break;
                case SceneType.Login: CurrentScene = LoginScene; break;
                case SceneType.SelectCountry: break;
                //case SceneType.SelectChr: CurrentScene = SelectChrScene; break;
                case SceneType.NewChr: break;
                case SceneType.Loading: break;
                //case SceneType.LoginNotice: CurrentScene = LoginNoticeScene; break;
                case SceneType.PlayGame: CurrentScene = PlayScene; break;
            }

            if (CurrentScene != null)
                CurrentScene.OpenScene();
        }

        public void RunScene(Sprite gameSprite)
        {
            CalculateFrameRate();

            //MSurface.Fill(0);

            if (CurrentScene != null)
                CurrentScene.PlayScene(gameSprite);
        }

        /// <summary>
        /// Calculates the frame rate for the game.
        /// </summary>
        /// <returns>Frame rate since last pass.</returns>
        private void CalculateFrameRate()
        {
            if (Environment.TickCount - LastTick >= 1000)
            {
                LastTick = Environment.TickCount;
                LastFrameRate = FrameRate;
                FrameRate = 0;
            }
            FrameRate++;
        }

        public void KeyPress(KeyPressEventArgs e) 
        {
            if (CurrentScene != null)
                CurrentScene.KeyPress(e);
        }

        public void KeyDown(KeyEventArgs e) 
        {
            if (CurrentScene != null)
                CurrentScene.KeyDown(e);
        }

        public void MouseMove(MouseEventArgs e) 
        {
            if (CurrentScene != null)
                CurrentScene.MouseMove(e);
        }

        public void MouseDown(MouseEventArgs e) 
        {
            if (CurrentScene != null)
                CurrentScene.MouseDown(e);
        }

    } 
    #endregion
}
